package com.freshmarket.freshmarket.utils;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * 加密工具
 */
public class EncryptUtil {

    public static final String ALGORITHM_NAME = "SHA-256";
    public static final int HASH_ITERATIONS = 1;

    /**
     * PWD_SALT_LENGTH: 密码加密盐值长度
     */
    public static final int PWD_SALT_LENGTH = 6;


    /**
     * 加密方法
     *
     * @param password
     * @return
     */
    public static String encrypt(String password, String salt) {
        return salt == null ? encrypt(password) : new SimpleHash(ALGORITHM_NAME, password, ByteSource.Util.bytes(salt), HASH_ITERATIONS).toHex();
    }

    public static String encrypt(String password) {
        return new SimpleHash(ALGORITHM_NAME, password, null, HASH_ITERATIONS).toHex();
    }

    /**
     * 生成盐值<br/>
     *
     * @return
     */
    public static String generateSalt() {
        return RandomStringUtils.randomAlphabetic(PWD_SALT_LENGTH);
    }

}
